import java.util.Arrays;
public class Test {
    public static void main(String[] args) {
        int[]nums = {1,9,2,8,3,7,4,6,5};
        //selectSort(nums);
        bubbleSort(nums);
        System.out.println(Arrays.toString(nums));
    }

    private static void bubbleSort(int[] nums) {
        for (int i = 0; i < nums.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < nums.length-1-i; j++) {
                if(nums[j]> nums[j+1]){
                    swap(nums,j,j+1);
                    flag = true;
                }
            }
            if(!flag){
                break;
            }
        }
    }

    private static void selectSort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < nums.length ; j++) {
                if(nums[j]<nums[minIndex]){
                    minIndex = j;
                }
            }
            swap(nums,minIndex,i);
        }
    }

    private static void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}
